<?php
class WorkApp extends CommonApp {
   
   	//创业栏目--学生界面
    public function studentAct(){
        $this->checkLogin();
        $memberid = My::arr2var($_SESSION,'member.memberid');
        $result = DB('workinfo')->clear()->where("userid={$memberid}")->find();
        $result['params'] = My::decodeJson($result['params']);
        $result['teacher'] = DB('homeuser')->clear()->where("type=1 AND power_1=2 AND power_2=2")->select();
        foreach ($result['teacher'] as &$value) {
            $value['params'] = My::decodeJson($value['params']);
        }
        //已有memberid，此处去除id
        unset($result['id']);
        $this->assign('result',$result);
        $this->display();
    }

   //创业栏目--学生界面
    public function teacherAct(){
        $this->checkLogin();
        $userid = My::arr2var($_SESSION,'member.memberid');
        $search['where'] = "teacher_id={$userid}";
        $keyword = I('get.keyword');
        if($keyword!='')
            $search['where'].=" AND company_name LIKE '%{$keyword}%'";
        $search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识
        $search['time'] = isset($_COOKIE['time_t_w']) ? $_COOKIE['time_t_w'] : '';
        $result=My::getCache($search,array('pre'=>'workinfo'));
        if($result == ''){
            $total = DB('workinfo')->where($search['where'])->cache()->count();
            $result['page'] = My::page($total,array('pagesize'=>1,'buttonNum'=>5));
            //数据
            $result['data'] = DB('workinfo')->where($search['where'])->limit($result['page']['limits'])->order('adddate DESC,id DESC')->cache()->select();
            unset($result['id']);
            My::setCache($search,$result,array('pre'=>'workinfo'));
        }
        //已有memberid，此处去除id
        $this->assign('result',$result);
        $this->display();
    }

    //创业学生信息
    public function student_infoAct(){
        $id = My::arr2var($_GET,'id');
        if(My::is_id($id)){
            $result = DB('homeuser')->clear()->where("id={$id}")->find();
            $result['params'] = My::decodeJson($result['params']);
        }
        $this->assign('result',$result);
        $this->display();
    }

    //创业导师信息
    public function teacher_infoAct(){
    	$this->display();
    }

    //创业公司信息
    public function company_infoAct(){
        $id = My::arr2var($_GET,'id');
        if(My::is_id($id)){
            $result = DB('workinfo')->clear()->where("id={$id}")->find();
            $result['params'] = My::decodeJson($result['params']);
        }
        $this->assign('result',$result);
        $this->display();
    }

    //创业信息验证
    public function workinfoAct(){
        $params = My::arr2var($_POST,'params');
        if(!My::is_string($params['name'],array('c')))
            $this->ajaxReturn(300,'姓名填写有误！');
        if(empty($params['class']))
            $this->ajaxReturn(300,'班级填写有误！');
        if(!My::is_date($params['birthdate']))
         $this->ajaxReturn(300,'出生日期填写有误！');
        if(!My::is_card($params['identify']))
            $this->ajaxReturn(300,'身份证填写有误！');
        if(!My::is_string($params['nation'],array('c')))
            $this->ajaxReturn(300,'民族填写有误！');
        if(!My::is_id($params['number']))
            $this->ajaxReturn(300,'学号填写有误！');
        if(!My::is_tel($params['mobile']))
            $this->ajaxReturn(300,'手机号码填写有误！');
        if(!My::is_email($params['email']))
            $this->ajaxReturn(300,'邮箱填写有误！');
        if(empty($params['address']))
            $this->ajaxReturn(300,'家庭地址不能为空！');
        if(empty($params['company_name']))
            $this->ajaxReturn(300,'公司名称不能为空！');
        if(empty($params['range']))
            $this->ajaxReturn(300,'经营范围不能为空！');
        if(!My::is_date($params['register_time']))
            $this->ajaxReturn(300,'注册时间填写有误！');
        if(!My::is_id($params['register_number']))
            $this->ajaxReturn(300,'注册号填写有误！');
        if(!My::is_price($params['register_money']))
            $this->ajaxReturn(300,'注册资金填写有误！');
        if(!My::is_id($params['member']))
            $this->ajaxReturn(300,'员工人数填写有误！');
        if(!My::is_id($params['size']))
            $this->ajaxReturn(300,'占地面积填写有误！');
        if(empty($params['company_type']))
            $this->ajaxReturn(300,'公司类型不能没空！');
        if(empty($params['company_address']))
            $this->ajaxReturn(300,'公司地址不能没空！');
        if(isset($_POST['params']['company']))
            foreach ($_POST['params']['company'] as $key => $value) {
                foreach ($value as $keys => $values) {
                   $_POST['params']['company'][$keys][$key] = $values;
                   unset($_POST['params']['company'][$key]);
                }
            }
        $userid = My::arr2var($_SESSION,'member.memberid');
        $result = DB('workinfo')->clear()->where("userid={$userid}")->find();
        $_POST['company_name'] = $params['company_name'];
        $_POST['student'] = $params['name'];
        if($result){
            $_POST['editdate'] = date("Y-m-d H:i:s");
            $ok = DB('workinfo')->clear()->where("userid={$userid}")->save($_POST);
                if($ok>=0)  $this->ajaxReturn(200,"创业信息修改成功");
                else        $this->ajaxReturn(300,"创业信息修改失败");
        }else{
            $_POST['adddate'] = date("Y-m-d H:i:s");
            $_POST['userid'] = $userid;
            $_POST['power'] = '0';
            $ok = DB('workinfo')->clear()->add($_POST);
            if($ok>=0)  $this->ajaxReturn(200,"创业信息添加成功");
            else        $this->ajaxReturn(300,"创业信息添加失败");
        }
    }

    //合伙人信息验证
    public function com_provingAct(){
        if(!My::is_string($_POST['name'],array('c')))
            $this->ajaxReturn(300,'姓名填写有误！');
        if(!My::is_tel($_POST['phone']))
            $this->ajaxReturn(300,'手机号码填写有误！');
        if(!My::is_string($_POST['xy_name'],array('c')))
            $this->ajaxReturn(300,'学院填写有误！');
        if(empty($_POST['grade']))
            $this->ajaxReturn(300,'班级填写有误！');
        $this->ajaxReturn(200,'添加合伙人成功！');
    }

    //创业信息打印界面
    public function print_workAct(){
        $memberid = My::arr2var($_SESSION,'member.memberid');
        $result = DB('workinfo')->clear()->where("userid={$memberid}")->find();
        $result['params'] = My::decodeJson($result['params']);
        //已有memberid，此处去除id
        unset($result['id']);
        $this->assign('result',$result);
        $this->display();
    }

    //创业导师申请
    public function tutorAct(){
        $this->display();
    }

    //同意成为指导老师
    public function checkAct(){
        $check = My::arr2var($_POST,'check');
        $ok = DB('workinfo')->clear()->where("id={$check}")->setField('power','1');
        $this->ajaxReturn(200,'您已同意成为该项目的指导老师');
    }
}
